package com.hundunjiang.ToolsServer.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.hundunjiang.CommonServer.api.CommonResult;
import com.hundunjiang.CommonServer.api.ResultCode;
import com.hundunjiang.CommonServer.server.RedisService;
import com.hundunjiang.ToolsServer.server.PhoneService;
import com.hundunjiang.ToolsServer.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "手机验证码")
@RequestMapping("/phone")
public class PhoneController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private PhoneService phoneService;

    @ApiOperation("获取手机验证码(注册)")
    @RequestMapping(path = "/get_phone_code",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult get_phone_code(
            @ApiParam(name = "phone", value = "手机号") @RequestParam("phone") String phone
    ){
        try{
            String code=redisService.get(phone);
            if (code==null){
                code= RandomUtil.getSixBitRandom();
                return phoneService.get_phone_code(phone,code);
            }else {
                return CommonResult.failed(ResultCode.FAILED,"验证码已发送请,稍后再试");
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }

    @ApiOperation("发送短信注册成功")
    @RequestMapping(path = "/send_regist_success",method = RequestMethod.GET)
    @ResponseBody
    public CommonResult send_regist_success(
            @ApiParam(name = "phone", value = "手机号") @RequestParam("phone") String phone,
            @ApiParam(name = "size", value = "MinIO空间大小(默认1GB)") @RequestParam(value = "size", required = false, defaultValue = "1") String size
    ){
        try{
            return phoneService.send_regist_success(phone,size);
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed();
        }
    }
}
